from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui

import sys


class QMenuBarTest(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(QMenuBarTest, self).__init__(parent)

        self.setWindowTitle("QMenuBarTest例子")

        _layout = QtWidgets.QHBoxLayout()
        _bar = self.menuBar()
        _file_menu = _bar.addMenu("File")
        _file_menu.addAction("New")

        _save = QtWidgets.QAction("Save", self)
        _save.setShortcut("Ctrl+S")
        _file_menu.addAction(_save)

        _edit_menu = _file_menu.addMenu("Edit")
        _edit_menu.addAction("copy")
        _edit_menu.addAction("paste")

        _quit = QtWidgets.QAction("Exit", self)
        _file_menu.addAction(_quit)

        _file_menu.triggered[QtWidgets.QAction].connect(
            self.handle_file_menu_action)
        self.setLayout(_layout)

    def handle_file_menu_action(self, item):
        print(item.text())
        if item.text() == "Exit":
            self.close()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    QtWidgets.QApplication.setStyle("Fusion")
    win = QMenuBarTest()
    win.show()
    sys.exit(app.exec_())
